//
//  UIViewController+Js.m
//  JSGategoryLib
//
//  Created by allumos on 03/27/2018.
//  Copyright (c) 2018 allumos. All rights reserved.
//

#import "UIViewController+Js.h"
#import <objc/runtime.h>

@implementation UIViewController (Js)

- (wj_jumpBlock)wj_jumpToIfCan{
    
    wj_jumpBlock jumpblock = ^ BOOL(UIViewController *willJumpVC)
    {
        UIViewController *topVC = [self wj_topestViewController];
        if (!topVC) {
            return NO;
        }else{
            [topVC.navigationController pushViewController:willJumpVC animated:YES];
        }
        return YES;
    };
    return jumpblock;
}

- (UIViewController *)wj_topestViewController
{
    UIViewController *topVc;
    
    return topVc;
}


#pragma mark - swizzle
+ (void)load
{
    //class_getInstanceMethod 也可以获取类方法,因为类本身也是对象.传入他的class 指针就可以获取类方法
    Method method1 = class_getInstanceMethod([self class], NSSelectorFromString(@"dealloc"));
    Method method2 = class_getInstanceMethod([self class], @selector(deallocSwizzle));
    method_exchangeImplementations(method1, method2);
    
#ifdef DEBUG
    //原本的viewWillAppear方法
    Method viewWillAppear = class_getInstanceMethod(self, @selector(viewWillAppear:));
    //需要替换成 能够输出日志的viewWillAppear
    Method logViewWillAppear = class_getInstanceMethod(self, @selector(logViewWillAppear:));
    //两方法进行交换
    method_exchangeImplementations(viewWillAppear, logViewWillAppear);    
#endif

}
///   warning  - 这里可以log 控制器销毁记录
- (void)logViewWillAppear:(BOOL)animated {
    
    NSString *className = NSStringFromClass([self class]);
    //在这里，你可以进行过滤操作，指定哪些viewController需要打印，哪些不需要打印
    if ([className hasPrefix:@"UI"] == NO) {
//        DLog(@"%@ will appear",className);
    }
    //下面方法的调用，其实是调用viewWillAppear
    [self logViewWillAppear:animated];
}
- (void)deallocSwizzle
{
//    DLog(@"%@被销毁了", self);
    
    [self deallocSwizzle];
}

static char MethodKey;
- (void)setMethod:(NSString *)method
{
    objc_setAssociatedObject(self, &MethodKey, method, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (NSString *)method
{
    return objc_getAssociatedObject(self, &MethodKey);
}


/**
 *  触摸屏幕隐藏键盘
 */
- (void)setUpForDismissKeyboard:(UIView *)selfView
{
    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    UITapGestureRecognizer *singleTapGR =
    [[UITapGestureRecognizer alloc] initWithTarget:self
                                            action:@selector(tapAnywhereToDismissKeyboard:)];
    NSOperationQueue *mainQuene =[NSOperationQueue mainQueue];
    [nc addObserverForName:UIKeyboardWillShowNotification
                    object:nil
                     queue:mainQuene
                usingBlock:^(NSNotification *note){
                    [selfView addGestureRecognizer:singleTapGR];
                }];
    [nc addObserverForName:UIKeyboardWillHideNotification
                    object:nil
                     queue:mainQuene
                usingBlock:^(NSNotification *note){
                    [selfView removeGestureRecognizer:singleTapGR];
                }];
}

/**
 *  触摸屏幕隐藏键盘的事件
 */
- (void)tapAnywhereToDismissKeyboard:(UIGestureRecognizer *)gestureRecognizer {
    //此method会将self.view里所有的subview的first responder都resign掉
    [self.view endEditing:YES];
}

#pragma -mark hidden tabbar & nav

/** hidden tabbar & nav **/
- (void)hiddenTabbar:(BOOL)value {
    [self.rdv_tabBarController setTabBarHidden:value animated:YES];
}
- (void)hiddenNav:(BOOL)value {
    [self.navigationController setNavigationBarHidden:value animated:NO];
}
@end
